package com.css.fxfzaqbz.modules.assess2.service.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess2.entity.KeyplaceRiskassessment;
import com.css.fxfzaqbz.modules.assess2.entity.KrTask;
import com.css.fxfzaqbz.modules.assess2.entity.UTask;
import com.css.fxfzaqbz.modules.assess2.entity.UrgencysettleData;
import com.css.fxfzaqbz.modules.assess2.repository.KeyplaceRiskassessmentRepository;
import com.css.fxfzaqbz.modules.assess2.repository.KrTaskRepository;
import com.css.fxfzaqbz.modules.assess2.service.KrTaskService;
import com.css.fxfzaqbz.modules.data.entity.ImportantActivity;
import com.css.fxfzaqbz.modules.data.service.ImportantActivityService;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import com.css.fxfzaqbz.util.PlatformSessionUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class KrTaskServiceImpl implements KrTaskService {
    @Resource
    private KrTaskRepository krTaskRepository;
    @Resource
    private ImportantActivityService importantActivityService;
    @Resource
    private KeyplaceRiskassessmentRepository keyplaceRiskassessmentRepository;

    //有用
    @Override
    public KrTask getKrTask(String activeId) {
        //判断数据库里是否已经存在这一个大表
        KrTask krTask = krTaskRepository.getTask(activeId);
        ImportantActivity importantActivity = importantActivityService.getImportantActivityById(activeId);
        if (krTask != null) {
            //已经存在
            krTask.setKtActiveid(activeId);
            krTask.setKtId(UUIDGenerator.getUUID());
            krTask.setKtOpinionStatus(importantActivity.getIaOpinionStatus());
            krTask.setKtOpinion(importantActivity.getIaOpinion());
  //          KeyplaceRiskassessment keyplaceRiskassessment = keyplaceRiskassessmentRepository.getKeyplaceRiskassessmentByRelation(activeId);
//            if (keyplaceRiskassessment == null) {
//                List<KeyplaceRiskassessment> list = new ArrayList<>();
//                krTask.setLists(list);
//                return krTask;
//            }
//            List<KeyplaceRiskassessment> list = new ArrayList<>();
//            list.add(keyplaceRiskassessment);
//            krTask.setLists(list);
            return krTask;
        } else {
            //不存在
            KrTask krTask1 = new KrTask();
            List<KeyplaceRiskassessment> list = new ArrayList<>();
            krTask1.setLists(list);
            krTask1.setKtOpinionStatus(importantActivity.getIaOpinionStatus());
            krTask1.setKtOpinion(importantActivity.getIaOpinion());
            return krTask1;
        }
    }

    @Override
    public void saveTask(ImportantActivity importantActivity1) {
        KrTask task = krTaskRepository.getTask(importantActivity1.getIaId());
        if (task == null) {
            KrTask krTask = new KrTask();
            krTask.setKtId(UUIDGenerator.getUUID());
            krTask.setContent("重点场所风险评估");
            krTask.setKtStatus(YNEnum.N.toString());
            krTask.setKtActiveid(importantActivity1.getIaId());
            krTask.setKtDelFlag(YNEnum.N.toString());
            krTask.setKtCreateTime(PlatformDateUtils.getCurrentTimestamp());
            krTask.setKtCreateUser(PlatformSessionUtils.getUserId());
            krTask.setKtOpinion(importantActivity1.getIaOpinion());
            krTaskRepository.saveKrTask(krTask);
        }

    }

    @Override
    public void upStatus(String id) {
        krTaskRepository.upStatus(id);

    }
}
